package com.bood.shimmer.common.api;

import com.bood.shimmer.common.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * <p>
 * 通用结果集封装
 * </p>
 *
 * @author：bood
 * @date：2020/9/25
 */
@ApiModel("Response Json")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 2088339427716969846L;

    /**
     * 成功
     */
    private final static String SUCCESS = "success";
    /**
     * 失败
     */
    private final static String FAIL = "fail";

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private Integer code;
    /**
     * 返回的数据
     */
    @ApiModelProperty("数据")
    private T data;
    /**
     * 提示信息
     */
    @ApiModelProperty("提示信息")
    private String msg;


    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, T data, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public void setCode(Integer code) {
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    public static <T> Result<T> ok() {
        return new Result(ResultEnum.DEAL_SUCCESS.code(), SUCCESS);
    }

    public static <T> Result<T> ok(T data) {
        return new Result(ResultEnum.DEAL_SUCCESS.code(), data, SUCCESS);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return new Result(ResultEnum.DEAL_SUCCESS.code(), data, msg);
    }

    public static <T> Result<T> error() {
        return new Result(ResultEnum.INTERNAL_SERVER_ERROR.code(), FAIL);
    }

    public static <T> Result<T> error(String msg) {
        return new Result(ResultEnum.INTERNAL_SERVER_ERROR.code(), msg);
    }

    public static <T> Result<T> error(Object data, String msg) {
        return new Result(ResultEnum.INTERNAL_SERVER_ERROR.code(), data, msg);
    }

    /**
     * <p>
     * 处理 boolean 类型结果
     *
     * e:
     * true || true || true -> true
     * false || false || false -> false
     * true || false || true -> false
     *
     * </p>
     *
     * @param res: boolean[]
     * @return：com.bood.smp.common.api.Result<T>
     * @author：bood
     * @date：2020/11/14
     */
    public static <T> Result<T> or(boolean... res) {
        for (int i = 0; i < res.length - 1; i++) {
            for (int j = 1; j < res.length; j++) {
                if (res[i] != res[j]) {
                    return new Result(ResultEnum.BODY_NOT_MATCH.code(), FAIL);
                }
            }
        }
        return new Result(ResultEnum.DEAL_SUCCESS.code(), SUCCESS);
    }

    /**
     * <p>
     * 返回自定义状态码,带数据,带提示信息
     * </p>
     *
     * @param code : 状态码
     * @param data : 返回的数据
     * @param msg  : 提示信息
     * @return：com.bood.boot.common.utils.R<T>
     * @author：bood
     * @date：2020/9/25
     */
    public static <T> Result<T> res(Integer code, Object data, String msg) {
        return new Result(code, data, msg);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", data=" + data +
                ", msg='" + msg + '\'' +
                '}';
    }

}


